//
//  JSArrowValue1Cell.swift
//  fho
//
//  Created by 郭颢源 on 2021/4/28.
//

import UIKit

class JSArrowValue1Cell: JSBaseViewCell {
    lazy var arrowButton : JSArrowButton = {
        var _arrowButton = JSArrowButton(type: .custom)
        _arrowButton.isUserInteractionEnabled = false
        _arrowButton.titleLabel?.font = self.textFont
        _arrowButton.setTitleColor(self.textClor, for: .normal)
        _arrowButton.setImage(self.arrowImage, for: .normal)
        
        return _arrowButton;
    }()
    lazy var arrowImage : UIImage = {
         var image = UIImage(named: "arrow_right")
         return image!
     }()
    lazy var line : UIView = {
        var line = UIView()
        line.backgroundColor = .systemGray6
        return line
    }()

    lazy var textField : UITextField = {
        var textField = UITextField()
        textField.font = self.textFont
        textField.textColor = self.textClor
        textField.textAlignment = .right
        textField.adjustsFontSizeToFitWidth = true
        textField.minimumFontSize = 11.0
        
        return textField
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(arrowButton)
        contentView.addSubview(line)
        contentView.addSubview(textField)
        self.arrowButton.snp.makeConstraints { (make) in
            make.left.equalTo(self.iconBtn.snp.right).offset(10)
            make.right.equalTo(self.contentView.snp.centerX).offset(30)
            make.centerY.equalTo(self.contentView.snp.centerY)
            make.height.equalTo(self.cellHeight - 20)
        }
        line.snp.makeConstraints { (make) in
            make.left.equalTo(arrowButton.snp.right).offset(5)
            make.height.equalToSuperview().multipliedBy(0.62)
            make.centerY.equalToSuperview()
            make.width.equalTo(1)
        }
        
        textField.snp.makeConstraints { (make) in
            make.right.equalTo(-self.cellMargin);
            make.left.equalTo(line.snp.right).offset(0)
            make.top.equalTo(self.contentView.snp.top).offset(0)
            make.bottom.equalTo(self.contentView.snp.bottom).offset(0)
        }
        
        
        textField.addTarget(self, action: #selector(textFieldTextChange(_:)), for: .editingChanged)
    }
    @objc func textFieldTextChange(_ textField: UITextField) {
        model.text1 = textField.text!
        self.parentVC.uploadDict[self.model.key1!] = textField.text
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func initWith<T>(Model formModel: JSFormModel, VC ViewController: T) where T : BaseFormsController {
        super.initWith(Model: formModel, VC: ViewController)
        
        let title = self.model.text.count>0 ? self.model.text:showTitleName()
        self.arrowButton.setTitle(title, for: .normal)
        let image = !self.model.showInfo ? self.arrowImage:nil
        self.arrowButton.setImage(image, for: .normal)
        
        let color = self.model.text.count>0 ? self.textClor:.lightGray
        self.arrowButton.setTitleColor(color, for: .normal)
        
    
        self.textField.keyboardType = UIKeyboardType(rawValue: model.keyBordType)!
        textField.placeholder = "请输入单价" //+ model.name
        textField.isUserInteractionEnabled = !model.showInfo
        textField.text = model.text1
    }
    

    
    override func doSomething() {
        if self.model.name.contains("日期") {

            JSFormPickerTools.showDateWithTitle(title: showTitleName()) { (selectedValue) in
                self.setArrowCellTextWith(text: selectedValue)
            }
        }else if self.model.name.contains("区域")
        {

            JSFormPickerTools.showAddressWithTitle(title: showTitleName()) { (selectedValue) in

                self.setArrowCellTextWith(text: selectedValue)
            }
        }else
        {
            
            JSFormPickerTools.showStringWithTitle(showTitleName(), dataSourceArr: self.model.texts) { (resultModel) in
                
                let text:String = resultModel.value!
                
                self.arrowButton.setTitle(text, for: .normal)
                self.arrowButton.setTitleColor(self.textClor, for: .normal)
                self.model.text = text
                
                self.parentVC.uploadDict[self.model.key!] = text
            }
        }
    }
    func setArrowCellTextWith(text:String) {
        self.arrowButton.setTitle(text, for: .normal)
        self.arrowButton.setTitleColor(self.textClor, for: .normal)
        self.model.text = text
    }
    
    func showTitleName() -> String {
        return "请选择"+self.model.name
    }


    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
